package com.itcast.controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 */
public class BeanServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过获取请求头的方式获取请求的url
        String uri = req.getRequestURI();
        //截取字符串，获取对应的方法名字
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        //有了方法名，先获取目标类的.class对象
        try {
            //再获取该.class对象来获取指定的方法
            Method declaredMethod =this.getClass().getDeclaredMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            //强拆
            declaredMethod.setAccessible(true);
            //执行
            declaredMethod.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
